import { addLayer, LayerType, LayerConfig } from "@/mapbox/mapbox_core/layer/layerManage";
import { getLayerManagerState } from "@/mapbox/mapbox_core/layer/layerManagerState";
/* eslint-disable @typescript-eslint/no-unused-vars */
// 解析，分配点线面
export function geojsonParse(map: any, layerId: string, geojsonData: any, defaultOptions: any) {
  // 添加 GeoJSON 数据源
  map.addSource(layerId, {
    type: "geojson",
    data: geojsonData
  });

  let layerOptions;
  const geometryType = getGeometryType(geojsonData);

  switch (geometryType) {
    case "Point":
      layerOptions = {
        type: "circle",
        paint: {
          "circle-color": defaultOptions.pointColor || "blue",
          "circle-radius": defaultOptions.pointRadius || 5
        },
        ...defaultOptions
      };
      break;

    case "LineString":
      layerOptions = {
        type: "line",
        paint: {
          "line-color": ["get", "valueColor"] || defaultOptions.lineColor || "green",
          "line-width": ["get", "lineWidth"] || defaultOptions.lineWidth || 2
        },
        ...defaultOptions
      };

      // 添加文字标签（symbol 图层）
      const textLayerId = `${layerId}-text`;
      const textLayer = {
        id: textLayerId,
        type: "symbol",
        source: layerId,
        layout: {
          "text-field": ["get", "value"] || "", // 从属性获取文字内容
          "text-font": ["MicrosoftYaHei"],
          "text-size": defaultOptions.textSize || 12,
          "text-anchor": "center", // 文字锚点位置
          "text-offset": [0, 0], // 文字偏移
          "text-rotate": ["get", "textRotate"] || 0, // 文字旋转角度
          "symbol-placement": "line-center", // 沿线路居中显示
          "symbol-spacing": 200, // 符号间距
          "text-allow-overlap": false, // 是否允许文字重叠
          "text-ignore-placement": false
        },
        paint: {
          "text-color": defaultOptions.textColor || "#333",
          "text-halo-color": defaultOptions.textHaloColor || "#fff",
          "text-halo-width": defaultOptions.textHaloWidth || 1,
          "text-opacity": defaultOptions.textOpacity || 1
        }
      };

      // 如果需要添加文字图层，返回两个图层配置
      if (defaultOptions.showText !== false) {
        map.addLayer(textLayer);
      }
      break;

    case "Polygon":
      layerOptions = {
        type: "fill",
        paint: {
          "fill-color": ["get", "fill"] || defaultOptions.fillColor || "red",
          "fill-opacity": ["get", "fill-opacity"] || defaultOptions.fillOpacity || 0.5,
          "fill-outline-color": ["get", "stroke"] || defaultOptions.strokeColor || "black",
          "fill-outline-opacity": ["get", "stroke-opacity"] || defaultOptions.strokeOpacity || 1
        },
        ...defaultOptions
      };
      break;

    default:
      console.error("Unsupported geometry type in GeoJSON");
      return;
  }

  // 添加基础图层
  map.addLayer({
    id: layerId,
    ...layerOptions,
    source: layerId
  });

  return {
    id: layerId,
    geometryType,
    layerOptions
  };
}

// 函数getGeometryType用于获取geojson对象的几何类型
function getGeometryType(geojson) {
  // 如果geojson对象的类型为FeatureCollection且features数组长度大于0
  if (geojson.type === "FeatureCollection" && geojson.features.length > 0) {
    // 返回features数组中第一个元素的geometry.type
    return geojson.features[0].geometry.type;
    // 如果geojson对象的类型为Feature
  } else if (geojson.type === "Feature") {
    // 返回geometry.type
    return geojson.geometry.type;
  }
  // 如果不符合上述条件，返回null
  return null;
}

// 加载geojson 图标
export function loadGeojsonIcon(map: any, id: string, geojsonData: any) {
  const source = {
    type: LayerType.geojson,
    data: geojsonData
  };
  // 添加图层显示点标记
  const layerOptions: LayerConfig = {
    id: id,
    type: LayerType.Symbol,
    source,
    layout: {
      "icon-image": "{iconName}", // 使用Mapbox默认图标
      "icon-size": 0.4
    },
    paint: {
      "icon-color": "#2262CC" // 自定义图标颜色
    }
  };
  addLayer(getLayerManagerState(map), layerOptions);
}


// 导出一个函数，用于生成线图层
export function generateLineLayer(
  layerId: string, 
  coordinates: Array<[number, number]>, 
  options: {
    lineColor?: string;
    lineWidth?: number;
    lineOpacity?: number;
    withText?: boolean;
    textField?: string;
    textSize?: number;
    textColor?: string;
    textOffset?: [number, number];
    sourceId?: string;
  } = {}
) {
  // 确保有足够的坐标点来形成线
  if (coordinates.length < 2) {
     console.log("当前创建坐标数量不足，来自数据处理geojson")
  }
  
  // 合并默认选项和用户提供的选项
  const defaultOptions = {
    lineColor: "#0066ff",
    lineWidth: 3,
    lineOpacity: 1,
    withText: false,
    textField: "线路",
    textSize: 12,
    textColor: "#ffffff",
    textOffset: [0, -1],
    sourceId: layerId,
  };
  
  const mergedOptions = { ...defaultOptions, ...options };
  
  // 创建GeoJSON特征
  const geojsonFeature = {
    type: "Feature",
    geometry: {
      type: "LineString",
      coordinates
    },
    properties: {}
  };
  
  // 创建GeoJSON数据源
  const source = {
    type: "geojson",
    data: {
      type: "FeatureCollection",
      features: [geojsonFeature]
    }
  };
  
  // 创建线图层配置
  const lineLayer: LayerConfig = {
    id: layerId,
    type: LayerType.Line,
    source,
    paint: {
      "line-color": mergedOptions.lineColor,
      "line-width": mergedOptions.lineWidth,
      "line-opacity": mergedOptions.lineOpacity
    }
  };
  
  // 创建文本图层配置(如果需要)
  let textLayer = null;
  if (mergedOptions.withText) {
    textLayer = {
      id: `${layerId}-text`,
      type: LayerType.Symbol,
      source,
      layout: {
        "text-field": mergedOptions.textField,
        "text-font": ["MicrosoftYaHei"],
        "text-size": mergedOptions.textSize,
        "text-anchor": "top",
        "text-offset": mergedOptions.textOffset,
        "text-allow-overlap": true,
        "symbol-placement": "line-center"
      },
      paint: {
        "text-color": mergedOptions.textColor,
        "text-halo-color": "#000000",
        "text-halo-width": 1,
        "text-opacity": 0.9
      }
    };
  }
  
  // 返回图层配置和数据源
  return {
    source,
    lineLayer,
    textLayer
  };
}
